package com.usdt.system.service.impl.cp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usdt.common.exception.ServiceException;
import com.usdt.common.utils.bean.BeanUtil;
import com.usdt.system.cp.utils.UserAndDeptUtils;
import com.usdt.system.domain.cp.ActivityAddressTempEntity;
import com.usdt.system.domain.cp.ActivityEntity;
import com.usdt.system.domain.req.ActivityAddressTempReqVO;
import com.usdt.system.domain.resp.ActivityAddressTempRespVO;
import com.usdt.system.mapper.cp.ActivityAddressTempMapper;
import com.usdt.system.service.cp.ActivityAddressTempService;

@Service
public class ActivityAddressTempServiceImpl extends ServiceImpl<ActivityAddressTempMapper, ActivityAddressTempEntity> 
											implements ActivityAddressTempService{
	
	@Value("${sys.properties.ky.main_dept_id}")
	public String mainDeptId;

	@Override
	public Integer insert(ActivityAddressTempReqVO vo) {
		ActivityAddressTempEntity entity = BeanUtil.toBean(vo, ActivityAddressTempEntity.class);
		Map<String, String> currentUserIdAndDeptId = UserAndDeptUtils.getCurrentUserIdAndDeptId();
		entity.setDeptId(currentUserIdAndDeptId.get("currentDeptId"));
		entity.setCreateBy(currentUserIdAndDeptId.get("currentUserId"));
		entity.setUpdateBy(currentUserIdAndDeptId.get("currentUserId"));
		baseMapper.insert(entity);
		
		return entity.getId();
	}

	@Override
	public List<ActivityAddressTempRespVO> allList(String name) {
		List<ActivityAddressTempRespVO> result = new  ArrayList<ActivityAddressTempRespVO>();
		String currentDeptId = UserAndDeptUtils.getCurrentDeptId();
		
		QueryWrapper<ActivityAddressTempEntity> query = new QueryWrapper<ActivityAddressTempEntity>();
		query.eq("status", (byte)1);
		if(StringUtils.isNotBlank(name)) {
			query.like("name", name);
		}
		if(!mainDeptId.equals(currentDeptId)) {
			query.eq("dept_id", currentDeptId);
		}
		List<ActivityAddressTempEntity> list = baseMapper.selectList(query);
		if(!CollectionUtils.isEmpty(list)) {
			result = BeanUtil.toBeans(list, ActivityAddressTempRespVO.class);
		}
		
		return result;
	}

	@Override
	public Integer del(Integer id) {
		ActivityAddressTempEntity entity = new ActivityAddressTempEntity();
		entity.setId(id);
		entity.setStatus((byte)2);
		Map<String, String> currentUserIdAndDeptId = UserAndDeptUtils.getCurrentUserIdAndDeptId();
		entity.setUpdateBy(currentUserIdAndDeptId.get("currentUserId"));
		baseMapper.updateById(entity);
		return id;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Object operation(List<String> vo) {
		Map<String, String> currentUserIdAndDeptId = UserAndDeptUtils.getCurrentUserIdAndDeptId();
		String currentDeptId = currentUserIdAndDeptId.get("currentDeptId");
		String currentUserId = currentUserIdAndDeptId.get("currentUserId");
		//删除
		QueryWrapper<ActivityAddressTempEntity> delQuery = new QueryWrapper<ActivityAddressTempEntity>();
		delQuery.eq("status", (byte)1).eq("dept_id", currentDeptId);
		baseMapper.delete(delQuery);
		
		if(CollectionUtils.isEmpty(vo)) {
			return null;
		}
		//去重
		List<String> collect = vo.stream().collect(Collectors.toSet()).stream().collect(Collectors.toList());
		if(collect.size() != vo.size()) {
			throw new ServiceException("活动地点存在重复", -1);
		}
		
		//新增
		if(!CollectionUtils.isEmpty(collect)) {
			for (String name : collect) {
				ActivityAddressTempEntity entity = new ActivityAddressTempEntity();
				entity.setName(name);
				entity.setDeptId(currentDeptId);
				entity.setCreateBy(currentUserId);
				entity.setUpdateBy(currentUserId);
				baseMapper.insert(entity);
			}
		}
		return null;
	}
}
